package com.ftg.learn.bus.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ftg.learn.bus.VO.UserRidVo;
import com.ftg.learn.bus.VO.UserRoleNameVo;
import com.ftg.learn.bus.entity.User;
import com.ftg.learn.bus.service.IUserService;
import com.ftg.learn.util.ExportExcelUtil;
import com.ftg.learn.util.ResEntity;
import com.ftg.learn.util.ResPageEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.*;

/**
 * <p>
 * 人员表 前端控制器
 * </p>
 *
 * @author lalala
 * @since 2022-03-07
 */
@RestController
@RequestMapping("/bus/user")
@Api(tags = "人员管理功能列表")
public class UserController extends CrudpController<User, IUserService> {


    @GetMapping("/showAddRole")
    @ApiOperation(value = "查询全部人员，及其角色(真)")
    public ResEntity showAddRole() {
        return new ResEntity(0, "success", super.service.showAddRole());
    }


    @GetMapping("/showAddRoleXML")
    @ApiOperation(value = "查询全部人员，及其角色XML")
    public ResEntity queryAllUsers() {
        return new ResEntity(0, "success", super.service.queryAllUsers());
    }

    @GetMapping("/showRoleForUserId")
    @ApiOperation(value = "查某个人具有的角色(真)")
    public ResEntity showRoleForUserId(@RequestParam Long uid) {
        return new ResEntity(0, "success", super.service.showRoleForUserId(uid));
    }

    @GetMapping("/showByName")
    @ApiOperation(value = "根据用户的名字进行模糊查询")
    public ResEntity showByName(@RequestParam String uname){
        return new ResEntity(0,"success",super.service.showByName(uname));
    }

    @PostMapping("/insertUser")
    @ApiOperation(value = "单表添加用户(真)")
    public ResEntity insertUser(@RequestBody User user) {
        return new ResEntity(0, "success", super.service.insertUser(user));
    }

    @PutMapping("/updateUser")
    @ApiOperation(value = "单表修改用户(真)")
    public ResEntity updateUser(@RequestBody User user) {
        return new ResEntity(0, "success", super.service.updateUser(user));
    }


    @PostMapping("/roleGroupOne")
    @ApiOperation(value = "对操作的用户删除角色并且添加新角色 (只操作一个角色)")
    public ResEntity roleGroupOne(@RequestParam Long rid, @RequestParam Long uid) {
        return new ResEntity(0, "success", super.service.roleGroupOne(rid, uid));
    }

    @PostMapping("/roleGroup")
    @ApiOperation(value = "对操作的用户删除角色并且添加新角色 (操作多个角色)")
    public ResEntity roleGroup(@RequestParam List<Long> rid, @RequestParam Long uid) {
        return new ResEntity(0, "success", super.service.roleGroup(rid, uid));
    }

    @PostMapping("/insertAddRole")
    @ApiOperation(value = "添加人员，并同时添加其角色(真)")
    public ResEntity insertAddRole(@RequestBody UserRidVo userRidVo) {
        return super.service.insertAddRole(userRidVo) ? super.resEntity.OK() : super.resEntity.Failure();
    }

    @DeleteMapping("/deleteById/{id}")
    @ApiOperation(value = "通过ID逻辑删除一个用户，并真删除其对应的用户-角色关联表数据。(真)")
    @Override
    public ResEntity deleteById(@PathVariable Long id) {
        return service.deleteAndRole(id) ? resEntity.OK() : resEntity.Failure("操作失败，请重试!");
    }

    @DeleteMapping("/deleteIdsAndRole")
    @ApiOperation(value = "批量删除功能")
    public ResEntity deleteIdsAndRole(@RequestParam List<Serializable> ids){
        return super.service.deleteIdsAndRole(ids)?resEntity.OK():resEntity.Failure("操作失败，请重试!");
    }


    @GetMapping("downLoadStuInfoExcel")
    public void downLoadStuInfoExcel(HttpServletResponse response, HttpServletRequest request) {
        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            Map<String, Object> hashMap = new HashMap<>();
            hashMap.put("name", "guoGuo" + i);
            hashMap.put("age", i);
            list.add(hashMap);
        }
        LinkedHashMap<String, Object> linkedHashMap = new LinkedHashMap<>();
        linkedHashMap.put("name", "姓名");
        linkedHashMap.put("age", "年龄");
        ExportExcelUtil.exportXls(list, linkedHashMap, "人员信息", response);
    }

    @PutMapping("/updateById")
    @ApiOperation(value = "修改功能")
    @Override
    public ResEntity updateById(@RequestBody User user){
        super.service.updateLocalTime(user.getUid());
        return service.updateById(user)?resEntity.OK():resEntity.Failure("操作失败，请重试!");
    }


    @GetMapping("/pageUserAndRoleName")
    @ApiOperation(value = "分页功能带角色")
    public ResPageEntity pageUserAndRoleName(@RequestParam int page, @RequestParam int limit){
        Page<UserRoleNameVo> p = new Page<>(page,limit);
        p.setRecords(super.service.showUserAndRoleName(p));
        return new ResPageEntity(p);
    }

    @GetMapping("/pageUserAndRoleNameByName")
    @ApiOperation(value = "按名字模糊查询分页功能带角色")
    public ResPageEntity pageUserAndRoleNameByName(@RequestParam String uname,@RequestParam int page, @RequestParam int limit){
        Page<UserRoleNameVo> p = new Page<>(page,limit);
        p.setRecords(super.service.pageUserAndRoleNameByName(p,uname));
        return new ResPageEntity(p);
    }

    @PutMapping("/resetPassword")
    @ApiOperation(value = "重置当前用户密码为88888888")
    public boolean resetPassword(int uid){
        return super.service.resetPassword(uid);
    }

    @GetMapping("/showAllRname")
    @ApiOperation(value = "获取当前所有的角色名称")
    public ResEntity showAllRname(){
        return new ResEntity().OK(super.service.showAllRname());
    }


}
